﻿using System;
using System.Text;

namespace Yz.Core.Extensions
{
    /// <summary>
    /// 邀请码生成（0-9，A-Z，去除了 0，1，O,I）
    /// </summary>
    public static class ShareCodeExtension
    {
        /// <summary>
        /// 自定义char数组
        /// </summary>
        private static readonly char[] charList = new char[]{'H', 'V', 'E', '8', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P',
            '5', 'I', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'W', 'Y', 'L', 'T', 'N', '6', 'B', 'G', 'Q'};

        /// <summary>
        /// A补位字符，不能与自定义重复
        /// </summary>
        private static readonly char stuffixChar = 'A';

        /// <summary>
        /// 进制长度
        /// </summary>
        private static readonly int binLength = charList.Length;

        /// <summary>
        /// ID转换为邀请码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="codeLength"></param>
        /// <returns></returns>
        public static String IdToCode(this int id, int codeLength = 6)
        {
            char[] buf = new char[binLength];
            int charPos = binLength;

            // 当id除以数组长度结果大于0，则进行取模操作，并以取模的值作为数组的坐标获得对应的字符
            while (id / binLength > 0)
            {
                int index = id % binLength;
                buf[--charPos] = charList[index];
                id /= binLength;
            }

            buf[--charPos] = charList[id % binLength];
            // 将字符数组转化为字符串
            String result = new String(buf, charPos, binLength - charPos);

            // 长度不足指定长度则随机补全
            int len = result.Length;
            if (len < codeLength)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(stuffixChar);
                Random random = new Random();
                // 去除SUFFIX_CHAR本身占位之后需要补齐的位数
                for (int i = 0; i < codeLength - len - 1; i++)
                {
                    sb.Append(charList[random.Next(binLength)]);
                }

                result += sb.ToString();
            }

            return result;
        }

        /// <summary>
        /// code转换成ID
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static int CodeToId(this string code)
        {
            char[] charArray = code.ToCharArray();
            int result = 0;
            for (int i = 0; i < charArray.Length; i++)
            {
                int index = 0;
                for (int j = 0; j < binLength; j++)
                {
                    if (charArray[i] == charList[j])
                    {
                        index = j;
                        break;
                    }
                }

                if (charArray[i] == stuffixChar)
                {
                    break;
                }

                if (i > 0)
                {
                    result = result * binLength + index;
                }
                else
                {
                    result = index;
                }
            }

            return result;

        }
    }
}
